import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.SpinnerNumberModel;
import javax.swing.table.DefaultTableModel;
public class TopVarBaisse extends javax.swing.JFrame {

    Administration adm;
    private String[][] data;
    int n; // nbre des actions 
    public TopVarBaisse(Administration ad) {
        initComponents();
        adm =ad;
        
        tabBaisse.setEnabled(false);
        n= DbManip.next_Id("action","id_action")-1;
        if(n==0){
            return;
        }
        data= new String[n][5];
        variation();        
        trierMatrice(data, 4);
    }
    public void trierMatrice(String [][] mat, int num){
        //int long = mat.length;
        boolean ok=false;
        while (ok ==false){
        for (int i=0;i<mat.length-1;i++){
            ok=true;       
            String val1= mat[i][num-1].toString();
            String val2= mat[i+1][num-1].toString();
                if (Float.valueOf(val1)<Float.valueOf(val2)){
                    ok=false;
                    // permutation des deux lignes
                    for(int k=0;k<5;k++){ 
                        String var;
                        var=mat[i][k];
                        mat[i][k]=mat[i+1][k];
                        mat[i+1][k]=var;
                    }
            }
        }
    }
    }
    public void variation(){
        for (int i=0;i<n;i++){
            try {
                String id = String.valueOf(i+1);     
                String nom_entreprise="0";
                DbManip.requete = "select ID_HIS,DATE_H_A ,VALEUR,NOM_ENTREPRISE from historique_action inner join entreprise on id_action = id_entreprise where id_action='"+id+"' order by DATE_H_A ";
                DbManip.executer(DbManip.requete);
                DbManip.rs.next();
                nom_entreprise=DbManip.rs.getString("NOM_ENTREPRISE");
                Float val= Float.valueOf(DbManip.rs.getString("VALEUR"));
                Float ancienneValeur = val;
                Float nouvelleValeur = val;
                java.sql.Timestamp dateChangement = new java.sql.Timestamp(0);
                dateChangement=DbManip.rs.getTimestamp("DATE_H_A");
                Float maxBaisse = Float.valueOf("0");
                while (DbManip.rs.next())
                {
                    Float valsuivant= Float.valueOf(DbManip.rs.getString("VALEUR"));
                    Float differ= valsuivant-val;
                    if(differ<maxBaisse){
                        ancienneValeur=val;
                        nouvelleValeur=valsuivant;
                        dateChangement=DbManip.rs.getTimestamp("DATE_H_A");
                        maxBaisse= differ;
                    } 
                    val=valsuivant;
                }
                if(maxBaisse!=0){
                maxBaisse*=-1;}
                data[i][0]=nom_entreprise.substring(0, nom_entreprise.indexOf("    "));
                data[i][1]=String.valueOf(ancienneValeur);
                data[i][2]=String.valueOf(nouvelleValeur);
                data[i][3]=String.valueOf(maxBaisse);
                data[i][4]=dateChangement.toString();
            }
            catch (SQLException ex) {
                Logger.getLogger(TopForteHausse.class.getName()).log(Level.SEVERE, null, ex);
            }
          
        }      
    }
    public void viderListe(JTable table){
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        int n=model.getRowCount();
        for(int i=0;i<n;i++){
            model.removeRow(n-i-1);
        }
    }
    public void remplirBaisse(int nb){
            viderListe(tabBaisse);          
            DefaultTableModel model1 = (DefaultTableModel) tabBaisse.getModel();
            int min=0;
            if(data.length<nb){
                min=data.length;
            }
            else{
                min=nb;
            }
            
            for (int l = 0; l <min ; l++) {
                model1.addRow(new String[]{data[l][0],data[l][1],data[l][2],data[l][3],data[l][4]});
            }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        spinBaisse = new javax.swing.JSpinner(new SpinnerNumberModel(1, 1, 100, 1));
        jSeparator4 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabBaisse = new javax.swing.JTable();
        valider = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel3.setFont(new java.awt.Font("Vani", 1, 14)); // NOI18N
        jLabel3.setText("Top des actions à plus forte baisse:  ");
        jLabel3.setPreferredSize(new java.awt.Dimension(264, 25));

        jSeparator4.setBackground(new java.awt.Color(0, 153, 153));
        jSeparator4.setForeground(new java.awt.Color(0, 0, 0));

        tabBaisse.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Entreprise", "Ancienne valeur", "Nouvelle valeur", "Variation", "Date"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tabBaisse);

        valider.setBackground(new java.awt.Color(255, 255, 255));
        valider.setText("Top des actions  ");
        valider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validerActionPerformed(evt);
            }
        });

        jSeparator5.setBackground(new java.awt.Color(0, 153, 153));
        jSeparator5.setForeground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addComponent(jSeparator5)
                            .addComponent(jSeparator4, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(265, 265, 265)
                        .addComponent(valider)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(156, 156, 156)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(spinBaisse, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(165, 165, 165)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 68, Short.MAX_VALUE)
                        .addComponent(valider, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(spinBaisse, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void validerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_validerActionPerformed

        if(n==0){return;};
        int h = (int) spinBaisse.getValue();
        remplirBaisse(h);




        // TODO add your handling code here:
    }//GEN-LAST:event_validerActionPerformed

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSpinner spinBaisse;
    private javax.swing.JTable tabBaisse;
    private javax.swing.JButton valider;
    // End of variables declaration//GEN-END:variables
}
